package com.yanqu.road.server.manger.config;

import com.yanqu.road.logic.bussiness.player.UserBeautyBussiness;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.enums.ePatronsSkillType;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.entity.minewar.MinePitInfo;
import com.yanqu.road.entity.minewar.MineStationInfo;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.player.UserPatronsSkill;
import com.yanqu.road.entity.servercenter.TitleInfo;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MineWarConfigMgr extends TempMgr {

	private static Map<Integer, Map<Integer, MineStationInfo>> mineStationMap = new ConcurrentHashMap<>();

	private static Map<Integer, Map<Integer, MinePitInfo>> minePitInfoMap = new ConcurrentHashMap<>();

	public static final int DISPATCH_MINE_NONE = 0;

	public static final int DISPATCH_MINE_PUBLIC = 1;

	public static final int DISPATCH_MINE_SELF = 2;

	@Override
	public boolean init() throws Exception {
		return reload();
	}

	@Override
	public boolean stop() {
		return false;
	}

	@Override
	public boolean save() {
		return false;
	}

	@Override
	public boolean reloadConfig() {
	/*	mineStationMap = MineWarBussiness.getMineStationInfoMap();
		if (mineStationMap.size() <= 0) {
			return false;
		}
		minePitInfoMap = initMinePitMap();
		if(minePitInfoMap.size() <= 0){
			return false;
		}*/
		return true;
	}

	@Override
	public boolean reloadData() throws Exception {
		return true;
	}

	public static Map<Integer, Map<Integer, MineStationInfo>> getMineStationMap() {
		return mineStationMap;
	}

	public static Map<Integer, MineStationInfo> getMineStationMapByType(int mineType){
		return mineStationMap.get(mineType);
	}

	public static MineStationInfo getMineStationInfo(int mineType, int mineStationId){
		if(mineStationMap.containsKey(mineType)){
			return mineStationMap.get(mineType).get(mineStationId);
		}
		return null;
	}

	public static Map<Integer, Map<Integer, MinePitInfo>> getMinePitInfoMap() {
		return minePitInfoMap;
	}

	public static MinePitInfo getMinePitInfo(int mineType, int pitId) {
		if(minePitInfoMap.containsKey(mineType)){
			return minePitInfoMap.get(mineType).get(pitId);
		}
		return null;
	}

	/**
	 * 按照ID规则生成矿坑地图信息
	 */
	private static Map<Integer, Map<Integer, MinePitInfo>> initMinePitMap() {
		Map<Integer, Map<Integer, MinePitInfo>> totalMinePitInfoMap = new ConcurrentHashMap<>();
		for(Map.Entry<Integer, Map<Integer, MineStationInfo>> dataEntry : MineWarConfigMgr.getMineStationMap().entrySet()){
			if(!totalMinePitInfoMap.containsKey(dataEntry.getKey())){
				totalMinePitInfoMap.put(dataEntry.getKey(), new ConcurrentHashMap<>());
			}
			int pitId = 1;
			for (MineStationInfo stationInfo : dataEntry.getValue().values()) {
				for (int index = 0; index < stationInfo.getPitCount(); index++) {
					MinePitInfo pitInfo = new MinePitInfo();
					pitInfo.setMineType(stationInfo.getMineType());
					pitInfo.setStationId(stationInfo.getStationId());
					pitInfo.setPitId(pitId);
					pitInfo.setPitIndex(index + 1);
					pitInfo.setOutputSpeedAddition(stationInfo.getOutputSpeedAddition() + stationInfo.getAddOutputSpeedAddition() * (stationInfo.getPitCount() - index - 1));
					pitId++;
					totalMinePitInfoMap.get(dataEntry.getKey()).put(pitInfo.getPitId(), pitInfo);
				}
			}
		}
		return totalMinePitInfoMap;
	}

	public static String getMinePitName(int mineType, int pitId, String language){
		MinePitInfo pitInfo = getMinePitInfo(mineType, pitId);
		if(null == pitInfo){
			return "";
		}
		MineStationInfo stationInfo = getMineStationInfo(mineType, pitInfo.getStationId());
		if(null == stationInfo){
			return "";
		}
		return StringUtils.format(MultipleLanguageMgr.getContent(stationInfo.getStationName(), language), stationInfo.getShowStationIndex()) + "#" + pitInfo.getPitIndex();
	}

	public static BigDecimal getMineOutput(long userId, int mineType, List<UserPatrons> patronsList, int mineAddition){
		if(null == patronsList){
			return BigDecimal.ZERO;
		}
		UserInfo userInfo = UserMgr.getUserInfo(userId);
		TitleInfo titleInfo = TitleMgr.getTitleInfo(userInfo.getTitleId());
		double skillAddition;
		GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
		Map<Integer, UserBeauty> beautyMap;
		if(null != player){
			skillAddition = player.getModule(BeautyModule.class).getIdentitySkillAddition(eSkillType.MineOutput.getValue(), mineType);
		}else {
			beautyMap = UserBeautyBussiness.getUserBeautyMap(userId);
			skillAddition = SkillMgr.getIdentitySkillAddition(beautyMap, eSkillType.MineOutput.getValue(), mineType);
		}
		BigDecimal totalValue = BigDecimal.valueOf(titleInfo.getMineBaseOutput());
		double patronsSkillAddition = 0;
		for(UserPatrons userPatrons : patronsList){//计算天赋加成
			for(UserPatronsSkill patronsSkill : userPatrons.getSkillList()){
				if(patronsSkill.getType() != ePatronsSkillType.Talent.getValue()){
					continue;
				}
				SkillInfo skillInfo = SkillMgr.getSkillInfo(patronsSkill.getSkillId());
				if(skillInfo.getType() == eSkillType.MineWarAddOutput.getValue()){
					patronsSkillAddition += SkillMgr.getSkillAddition(skillInfo, patronsSkill.getSkillLv());
				}
			}
		}
		totalValue = totalValue.multiply(BigDecimal.valueOf((double)mineAddition / 1000 + 1)).setScale(0, BigDecimal.ROUND_UP);
		totalValue = totalValue.multiply(BigDecimal.valueOf((patronsSkillAddition + skillAddition) / 1000 + 1)).multiply(BigDecimal.valueOf(patronsList.size())).
				setScale(0, BigDecimal.ROUND_UP);
		return totalValue;
	}
}